package com.ruoyi.common.core.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Data
public class BatchResultDto<T> {
    private List<T> success;
    private List<Failed<T>> failed;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Failed<T> {
        private T item;
        private String msg;
    }

    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    public static class Builder<T> {
        private final List<T> success;
        private final List<Failed<T>> failed;

        public Builder() {
            this.success = new ArrayList<>();
            this.failed = new ArrayList<>();
        }

        public Builder(int initialCapacity) {
            this.success = new ArrayList<>(initialCapacity);
            this.failed = new ArrayList<>(initialCapacity);
        }

        public Builder<T> addSuccess(T t) {
            success.add(t);
            return this;
        }

        public Builder<T> addFailed(T t, String msg) {
            failed.add(new Failed<>(t, msg));
            return this;
        }

        public BatchResultDto<T> build() {
            BatchResultDto<T> dto = new BatchResultDto<>();
            dto.setSuccess(success);
            dto.setFailed(failed);
            return dto;
        }
    }
}
